﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mis_function : System.Web.UI.Page
{
  MISEntities dc = new MISEntities();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {      
      TreeViewUtil tvu = new TreeViewUtil(treeview_function);
      tvu.BuildTreeView(null);
    }
  }


  protected void btnAddRoot_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      AddNode("Root");
    }
  }
  protected void btnAddChild_Click(object sender, EventArgs e)
  {
    if (Page.IsValid)
    {
      AddNode("Child");
    }
  }

  protected void AddNode(string AddType)
  {
    try
    {
      if (!CheckDistinctNameOrUrlPath())
      {
        return;
      }
      using (MISEntities dc = new MISEntities())
      {
        MIS_FUNCTION_AP function = new MIS_FUNCTION_AP();
        function.FUNCTION_ID = DateTime.Now.ToString("yyyyMMdd-hhmmss-ff");
        function.FUNCTION_NAME = txt_function_name.Text.Trim();
        function.FUNCTION_TYPE = ddl_type_list.SelectedValue;
        function.FUNCTION_PATH = txt_navigate_url.Text.Trim();
        function.FUNCTION_SORT_ORDER = txt_sort_order.Text.Trim();
        function.FUNCTION_PARENT_ID = AddType == "Child" ? txt_parent_id.Text.Trim() : "999999-999999-99";
        function.FUNCTION_TARGET = ddl_target.SelectedValue;
        function.IS_DEFULT_AUTH = ddl_isdefault.SelectedValue;

        function.CRT_ID = User.Identity.Name;
        function.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
        function.CRT_TIME = DateTime.Now.ToString("hhmmss");
        dc.MIS_FUNCTION_AP.Add(function);
        dc.SaveChanges();

        TreeViewUtil tvu = new TreeViewUtil(treeview_function);
        tvu.BuildTreeView(null);
        treeview_function.ExpandAll();
      }
      CommonUtil.DisplayErrorMessage("新增成功!", lbl_message);
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_message);
    }
  }

  protected void btnUpdate_Click(object sender, EventArgs e)
  {
    try
    {
      if (treeview_function.SelectedNode != null)
      {
        if (Page.IsValid)
        {
          using (MISEntities dc = new MISEntities())
          {
            MIS_FUNCTION_AP function = dc.MIS_FUNCTION_AP
                               .Where(f => f.FUNCTION_ID == treeview_function.SelectedNode.Value)
                               .Single();
            if (function != null)
            {
              if (!CheckDistinctNameOrUrlPath(function.FUNCTION_ID))
              {
                return;
              }

              function.FUNCTION_NAME = txt_function_name.Text.Trim();
              function.FUNCTION_TYPE = ddl_type_list.SelectedValue;
              function.FUNCTION_PATH = txt_navigate_url.Text.Trim();
              function.FUNCTION_SORT_ORDER = txt_sort_order.Text.Trim();
              function.FUNCTION_PARENT_ID = txt_parent_id.Text.Trim();
              function.FUNCTION_TARGET = ddl_target.SelectedValue;
              function.IS_DEFULT_AUTH = ddl_isdefault.SelectedValue;
              function.UPD_ID = User.Identity.Name;
              function.UPD_DATE = DateTime.Now.ToString("yyyyMMdd");
              function.UPD_TIME = DateTime.Now.ToString("hhmmss");
              dc.SaveChanges();
            }
            CommonUtil.DisplayErrorMessage("更新成功!", lbl_message);
            TreeViewUtil tvu = new TreeViewUtil(treeview_function);
            tvu.BuildTreeView(null);
            treeview_function.ExpandAll();
          }
        }
      }
      else
      {
        CommonUtil.DisplayErrorMessage("請選取選單，以便進行更新!", lbl_message);
      }
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_message);
    }
  }
  protected void btnDelete_Click(object sender, EventArgs e)
  {
    try
    {
      if (treeview_function.SelectedNode != null)
      {
        if (treeview_function.SelectedNode.ChildNodes.Count > 0)
        {
          CommonUtil.DisplayErrorMessage("此節點尚有子節點，無法刪除!", lbl_message);
          return;
        }

        using (MISEntities dc = new MISEntities())
        {
          MIS_FUNCTION_AP function = dc.MIS_FUNCTION_AP
                             .Where(f => f.FUNCTION_ID == treeview_function.SelectedNode.Value)
                             .Single();
          dc.MIS_FUNCTION_AP.Remove(function);
          dc.SaveChanges();
          CommonUtil.DisplayErrorMessage("刪除成功!", lbl_message);
          TreeViewUtil tvu = new TreeViewUtil(treeview_function);
          tvu.BuildTreeView(null);
          treeview_function.ExpandAll();
        }
      }
      else
      {
        CommonUtil.DisplayErrorMessage("請選取選單，以便進行刪除!", lbl_message);
      }
    }
    catch (Exception ex)
    {
      CommonUtil.DisplayErrorMessage(ex.Message, lbl_message);
    }

  }
  protected void btnRefresh_Click(object sender, EventArgs e)
  {
    TreeViewUtil tvu = new TreeViewUtil(treeview_function);
    tvu.BuildTreeView(null);
    treeview_function.ExpandAll();
    lbl_function_id.Text = DateTime.Now.ToString("yyyyMMdd-hhmmss-ff");
    txt_function_name.Text = string.Empty;
    txt_parent_id.Text = string.Empty;
    txt_sort_order.Text = "1";
    txt_navigate_url.Text = string.Empty;
    ddl_target.SelectedValue = "none";
    ddl_type_list.SelectedValue = "MENU";
    ddl_isdefault.SelectedValue = "Y";
  }
  protected void treeview_function_SelectedNodeChanged(object sender, EventArgs e)
  {
    if (treeview_function.SelectedNode != null)
    {
      using (MISEntities dc = new MISEntities())
      {
        var function = dc.MIS_FUNCTION_AP
            .Where(f => f.FUNCTION_ID == treeview_function.SelectedValue)
            .Single();
        lbl_function_id.Text = function.FUNCTION_ID.Trim();
        txt_function_name.Text = function.FUNCTION_NAME;
        ddl_type_list.SelectedValue = function.FUNCTION_TYPE;
        txt_navigate_url.Text = function.FUNCTION_PATH;
        txt_sort_order.Text = function.FUNCTION_SORT_ORDER;
        txt_parent_id.Text = function.FUNCTION_PARENT_ID.Trim();
        ddl_target.SelectedValue = function.FUNCTION_TARGET;
        ddl_isdefault.SelectedValue = function.IS_DEFULT_AUTH;
      }
    }
  }

  protected bool CheckDistinctNameOrUrlPath()
  {
    bool IsChecked = true;
    //檢核選單名稱不能重複
    if (!string.IsNullOrEmpty(txt_function_name.Text.Trim()))
    {
      var counter =
        dc.MIS_FUNCTION_AP.Where(f => f.FUNCTION_NAME == txt_function_name.Text.Trim()).LongCount();
      if (counter > 0)
      {
        CommonUtil.DisplayErrorMessage("選單名稱不能重複!", lbl_message);
        IsChecked = false;
      }
    }
    //檢核選單URL不能重複
    if (!string.IsNullOrEmpty(txt_navigate_url.Text.Trim()))
    {
      var counter =
         dc.MIS_FUNCTION_AP.Where(f => f.FUNCTION_PATH == txt_navigate_url.Text.Trim()).LongCount();
      if (counter > 0)
      {
        CommonUtil.DisplayErrorMessage("選單 URL PATH 不能重複!", lbl_message);
        IsChecked = false;
      }
    }
    return IsChecked;
  }

  protected bool CheckDistinctNameOrUrlPath(string functionID)
  {
    bool IsChecked = true;
    //檢核除欲更新的選單之外，名稱不能重複
    if (!string.IsNullOrEmpty(txt_function_name.Text.Trim()))
    {
      var counter =
        dc.MIS_FUNCTION_AP
        .Where(f => f.FUNCTION_NAME == txt_function_name.Text.Trim() && f.FUNCTION_ID != functionID)
        .LongCount();
      if (counter > 0)
      {
        CommonUtil.DisplayErrorMessage("選單名稱不能重複!", lbl_message);
        IsChecked = false;
      }
    }
    //檢核除欲更新的選單之外，URL不能重複
    if (!string.IsNullOrEmpty(txt_navigate_url.Text.Trim()))
    {
      var counter =
         dc.MIS_FUNCTION_AP
         .Where(f => f.FUNCTION_PATH == txt_navigate_url.Text.Trim() && f.FUNCTION_ID != functionID)
         .LongCount();
      if (counter > 0)
      {
        CommonUtil.DisplayErrorMessage("選單 URL PATH 不能重複!", lbl_message);
        IsChecked = false;
      }
    }
    return IsChecked;
  }
}